package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repostiory.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository repository;
    @Autowired
    private IRegionService regionService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Destination getOne(String id) {

        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return repository.findAll();
    }

    @Override
    public void Update(String id,String info) {
        Destination destination = repository.findById(id).orElse(null);
        if (destination!=null){
            destination.setInfo(info);
            repository.save(destination);
        }



    }

    @Override
    public Destination queryName(String keyword) {
            return repository.queryByName(keyword);
    }


    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    /**
     * 区域查子目的地
     * @param rid
     * @return
     */
    @Override
    public List<Destination> getDestByRegionId(String rid) {
        Region region = regionService.getOne(rid);
        List<String> refIds = region.getRefIds();
        List<Destination> list = repository.findByIdIn(refIds);

        return list;
    }




    @Override
    public Page<Destination> query(DestinationQuery qo) {
        //查询条件对象
        Query query = new Query();
        if (StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        } else if (StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }
        //分页条件
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");


        return   DBHelper.query(mongoTemplate,Destination.class,query,pageable);
    }

    /**
     * 面包屑
     * @param parentId
     * @return
     */
    @Override
    public List<Destination> queryToasts(String parentId) {

        ArrayList<Destination> list = new ArrayList<>();

            this.createToast(list,parentId);
                //集合反转
            Collections.reverse(list);

        return list;
    }



    private void createToast(ArrayList<Destination> list, String parentId) {
        if (!StringUtils.hasLength(parentId)){
            return;
        }

        Destination destination = this.getOne(parentId);
        list.add(destination);
        if (StringUtils.hasLength(destination.getParentId())){
            createToast(list,destination.getParentId());
        }
    }

    @Override
    public List<Destination> queryDestByRegionIdForApi(String regionId) {
        List<Destination> list= new ArrayList<>();
       if ("-1".equals(regionId)){
           list =repository.findByParentName("中国");
       }else {

           list=this.getDestByRegionId(regionId);
       }
        for (Destination destination : list) {
            Pageable pageable=PageRequest.of(0,5,Sort.Direction.ASC,
                    "_id");
            List<Destination> children = repository.findByParentId(destination.getId(),pageable);
            destination.setChildren(children);
        }


        return list;
    }
}


































































